package com.compass.mobile.ui.activity

import UserInfoManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.databinding.ActivityInspectionOrderBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.constant.INSPECT_ORDER
import com.compass.mobile.framework.constant.TEST_ORDER_ACTION
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.DateUtils
import com.compass.mobile.framework.utils.LiveDataBus
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Inspection
import com.compass.mobile.models.ProjectRespVOS
import com.compass.mobile.presenters.InspectionItemPresenter
import com.compass.mobile.ui.dialog.CodeDialog
import com.compass.mobile.ui.dialog.CustomerServiceCode
import com.compass.mobile.ui.viewModel.InquiryViewModel
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 检验检查订单详情
 * Date:2024/7/3 19:14
 */
class InspectionOrderDetailsActivity : BaseVBActivity<ActivityInspectionOrderBinding>() {

    companion object {
        fun start(context: Context, orderId: String) {
            val intent = Intent(context, InspectionOrderDetailsActivity::class.java)
            intent.putExtra("orderId", orderId)
            context.startActivity(intent)
        }
    }

    private val inquiryViewModel: InquiryViewModel by viewModels()
    private var orderId: String? = null

    lateinit var oderCodeDialog: CodeDialog.Builder
    override fun initView(savedInstanceState: Bundle?) {
        orderId = intent.getStringExtra("orderId")

    }

    override fun initData() {
        inquiryViewModel.geInspectionOrder(orderId)
        inquiryViewModel.inspectionDetailsLiveData.observe(this) {
            it?.let {
                setOrderInfo(it)
            }
        }

        LiveDataBus.get().with(TEST_ORDER_ACTION, String::class.java)
            .observe(this) {
                if (::oderCodeDialog.isInitialized) {
                    oderCodeDialog.dismissDialog()
                }
                inquiryViewModel.geInspectionOrder(orderId)
                CustomerServiceCode.Builder(this, TEST_ORDER_ACTION).show()
            }
    }

    override fun intListener() {
        mBinding.tvGoToPay.onClick {
            val url =
                INSPECT_ORDER + UserInfoManager.getString(YD_USER_ID)
            oderCodeDialog = CodeDialog.Builder(this, url)
            oderCodeDialog.show()
        }
    }

    private fun setOrderInfo(order: Inspection) {
        mBinding.tvOrderId.text = "订单编号: ${order.id}"
        mBinding.tvStatus.text = "订单状态: ${UserInfoManager.getInspectionTypeString(order.status)}"
        mBinding.tvPatientName.text = "患者姓名: ${order.patientName}"
        mBinding.tvTime.text =
            "创建时间: ${DateUtils.conversionTime(order.createTime, DateUtils.FORMAT_TWO)}"
        specialColumn(order.projectRespVOS)

        if (order.status=="0") mBinding.tvGoToPay.visible() else mBinding.tvGoToPay.gone()


    }


    fun specialColumn(list: MutableList<ProjectRespVOS>?) {
        mBinding.caseItem.setNumColumns(2)
        mBinding.caseItem.verticalSpacing = dpToPx(10)
        val arrayObjectAdapter = ArrayObjectAdapter(InspectionItemPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            this,
            arrayObjectAdapter, null
        )
        mBinding.caseItem.setAdapter(itemBridgeAdapter)
        arrayObjectAdapter.addAll(0, list)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )


    }

}